package com.pethospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pethospital.common.Result;
import com.pethospital.dto.ReviewDTO;
import com.pethospital.entity.Review;
import com.pethospital.service.ReviewService;
import com.pethospital.service.AppointmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 评价控制器
 */
@RestController
@RequestMapping("/review")
@Slf4j
public class ReviewController {
    
    @Resource
    private ReviewService reviewService;
    
    @Resource
    private AppointmentService appointmentService;
    
    /**
     * 添加评价
     */
    @PostMapping
    public Result<Long> addReview(@RequestBody @Valid ReviewDTO reviewDTO, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        log.info("添加评价, 用户ID: {}, 数据: {}", userId, reviewDTO);
        
        Review review = new Review();
        BeanUtils.copyProperties(reviewDTO, review);
        
        Long reviewId = reviewService.addReview(review, userId);
        return Result.success(reviewId);
    }
    
    /**
     * 获取预约评价
     */
    @GetMapping("/appointment/{appointmentId}")
    public Result<Review> getReviewByAppointmentId(@PathVariable Long appointmentId) {
        log.info("获取预约评价, 预约ID: {}", appointmentId);
        
        Review review = reviewService.getReviewByAppointmentId(appointmentId);
        return Result.success(review);
    }
    
    /**
     * 获取用户评价列表
     */
    @GetMapping("/user")
    public Result<Page<Review>> getUserReviews(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        log.info("获取用户评价列表, 用户ID: {}, 页码: {}, 每页: {}", userId, page, size);
        
        Page<Review> reviews = reviewService.getUserReviews(userId, page, size);
        return Result.success(reviews);
    }
    
    /**
     * 获取医护人员评价列表
     */
    @GetMapping("/staff")
    public Result<Page<Review>> getStaffReviews(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {
        Long staffId = (Long) request.getAttribute("userId");
        log.info("获取医护人员评价列表, 医护人员ID: {}, 页码: {}, 每页: {}", staffId, page, size);
        
        Page<Review> reviews = reviewService.getStaffReviews(staffId, page, size);
        return Result.success(reviews);
    }
} 